from flask import Flask, render_template,request
app = Flask(__name__)

@app.route('/')
def index():
    return render_template("login.html")

@app.route('/dashboard')
def dashboard():
    return render_template("dashboard.html")

@app.route('/register')
def register():
    return render_template("register.html")

#处理登录逻辑
# 如果因为用户ID不存在登录失败,返回注册页面
# 如果因为用户密码错误登录失败,返回登录失败提醒页面
@app.route('/login',methods=['POST'])
def login():
    # 拿到浏览器端传过来的用户名和密码
    userid = request.form.get('userid')
    password = request.form.get('password')

    #users是从数据库取回来的用户列表
    users = [
        {'username': "damu","userid":"2024001", 'password': "123", 'email': "damu@iflytek.com"},
        {'username': "zk", "userid": "2024002", 'password': "456", 'email': "zk@iflytek.com"},
        {'username': "hf", "userid": "2024003", 'password': "789", 'email': "hf@iflytek.com"},
        {'username': "cjy", "userid": "2024004", 'password': "111", 'email': "cjy@iflytek.com"}
    ]

    #url:最终要去的页面
    url = ""
    # 拿到浏览器端传过来的用户名和密码,要去数据库中检验是否存在该用户
    users_length = len(users)
    i = 0
    while i < users_length:
        #i的有效值: 0 1 2 3
        if users[i]["userid"] == userid :
            # 说明数据库中存在当前用户, 查看密码是否正确 , 跳出当前循环
            if users[i]["password"] == password :
                #说明密码正确,返回登录成功,
                url = "login_success.html"
            else:
               # 说明密码正确,返回登录成功,跳出当前循环
               url = "login_error.html"
            break
        i = i + 1

    #整个while循环走完,url还是空的话,说明数据库中不存在当前用户
    if url == "":
        url = "register.html"

    return render_template(url)

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')